package com.heatup.apt.api.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.api.service.InviteCouponService;
import com.heatup.apt.common.dao.ActivityInfoMapper;
import com.heatup.apt.common.dao.ActivityUserMapper;
import com.heatup.apt.common.dao.CouponDetailMapper;
import com.heatup.apt.common.dao.InviteCouponMapper;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.dao.VipPurchaseLogMapper;
import com.heatup.apt.model.coupon.ActivityInfo;
import com.heatup.apt.model.coupon.ActivityUser;
import com.heatup.apt.model.coupon.InviteCoupon;
import com.heatup.apt.model.order.VipPurchaseLog;

/**
 *
 * InviteCoupon 表数据服务层接口实现类
 *
 */
@Service
public class InviteCouponServiceImpl extends SuperServiceImpl<InviteCouponMapper, InviteCoupon> implements InviteCouponService {
	
	private static final Logger logger = LoggerFactory.getLogger(InviteCouponServiceImpl.class);
	
	@Autowired
	private ActivityInfoMapper activityInfoMapper;
	
	@Autowired
	private ActivityUserMapper activityUserMapper;
	
	@Autowired
	private InviteCouponMapper inviteCouponMapper;
	
	@Autowired
	private OrderInfoMapper orderInfoMapper;
	
	@Autowired
	private CouponDetailMapper couponDetailMapper;
	
	@Autowired
	private VipPurchaseLogMapper vipPurchaseLogMapper;
	
	/**
	 * 邀请好友送优惠券扫描事件插入中间表
	 */
	@Override
	public synchronized Integer addInviteCouponByUserIdAndActivityId(Integer userId, Integer activityId,Integer shareUserId) {
		logger.info("=================================== addInviteCouponByUserIdAndActivityId start ===================");
		logger.info("===================================addInviteCouponByUserIdAndActivityId parameter：userId="+userId+" activityId="+activityId+" ===================");
		Integer addInviteCouponCount = 0 ;// 插入中间表的数量
		try {
			
			// 判断该用户是否有购买行为
			Integer countOrder = orderInfoMapper.queryUserOrderIsCompleted(userId);
			if(countOrder > 0){
				return 0;
			}
			// 判断该用户是否领取过该活动的优惠券
			ActivityInfo at = activityInfoMapper.selectById(activityId);
			if(at != null){
				String coupons = at.getInviteCouponId();
				List<String> lists = Arrays.asList(coupons.split("#"));
				Integer count = couponDetailMapper.queryUserHasCount(userId, lists);
				if(count > 0){
					return 0;
				}
			}
			// 没有成为会员过的记录
			List<VipPurchaseLog> vipLogList = vipPurchaseLogMapper.queryVipPurchaseLogByUserId(userId);
			if(vipLogList.size() > 0){
				return 0;
			}
			
			Long runStartTime = System.currentTimeMillis();
			ActivityInfo activityInfo = activityInfoMapper.selectById(activityId);
			// 插入被邀请者优惠券中间表
			InviteCoupon inviteCoupon = new InviteCoupon();
			inviteCoupon.setCouponId(activityInfo.getInviteCouponId());
			inviteCoupon.setUserId(userId);
			inviteCoupon.setIsGeted(0);
			inviteCoupon.setCreatedTime(new Date());
			addInviteCouponCount = inviteCouponMapper.insert(inviteCoupon);
			logger.info("===================================addInviteCouponByUserIdAndActivityId addInviteCouponCount "+addInviteCouponCount+" ===================");
			
			// 插入用户活动表 用于在用户首次完成训练之后给邀请者送优惠券
			ActivityUser activityUser = new ActivityUser();
			activityUser.setUserid(shareUserId);
			activityUser.setActivityId(activityId);
			activityUser.setInviteUserid(userId);
			activityUser.setCreateTime(new Date());
			activityUser.setCreateBy(userId+"");
			activityUser.setIsGetedCoupon(0);
			Integer addActiviUserCount = activityUserMapper.addActivityUserNotRepeat(activityUser);
			logger.info("===================================addInviteCouponByUserIdAndActivityId addActiviUserCount "+addActiviUserCount+" ===================");
			return addInviteCouponCount;
			
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("=========================addInviteCouponByUserIdAndActivityId error:"+e.getMessage()+" ===================");
			addInviteCouponCount = -1;
		}
		logger.info("=================================== addInviteCouponByUserIdAndActivityId end ===================");
		return addInviteCouponCount;
	}


}